#!/usr/local/bin/python3.7

import argparse
import sys, os
import pandas as pd
import numpy as np
from datetime import datetime
# sys.path.append('/public/lib')
# import npfac, cal
# from fees import Fees

# warnings.simplefilter('ignore')

print(sys.version)
exec_path = "./"
# ctp_path = exec_path+date+'_ctp.csv'
stock_path_sse = '20220712_sse_snap.csv'
stock_path_sze = '20220712_sze_snap.csv'
#stock_path_sze = exec_path+date+'_sze_snap.csv'

def time_form(x):
    if x[0:1] == '9':
        x = '0'+ x
        #return x[0:1] + ':' + x[1:3] + ':'+ x[3:5] #+ '.' +  x[6:] 
    return  x[0:2] + ':' + x[2:4] + ':'+ x[4:6] + '.' + x[6:]  

def time_to_micro(x):
    return x
    date_obj = datetime.fromisoformat(x)
    micro = date_obj.timestamp()*1000 
    return micro
 

def sze_time_to_micro(x):
    strs = x[0:4] + "-" + x[4:6] + "-" + x[6:8] + " " + x[8:10] + ":" + x[10:12]  + ":" + x[12:14] + "." + x[14:]
    return strs
   # print(strs)
    date_obj = datetime.fromisoformat(strs)
    micro = date_obj.timestamp()*1000
    return  micro


def form_sse(sse_file):
    sse_df = pd.read_csv(sse_file,header = None, sep=',', dtype=str) 
    sse_df['time'] = sse_df.iloc[:, 13]
    sse_df['ticker'] = sse_df.iloc[:,14]
    sse_df['ticker'] = sse_df['ticker'].apply(lambda x: 'sh.' + x)

    sse_df['last_price'] = pd.to_numeric(sse_df.iloc[:,22])
    sse_df['last_price'] = sse_df['last_price'].apply(lambda x: float(x)/1000 )

    sse_df['volume'] = pd.to_numeric(sse_df.iloc[:,28])
    sse_df['volume'] = sse_df['volume'].apply(lambda x: int(x)/1000 )

    sse_df['ask']    = pd.to_numeric(sse_df.iloc[:,39])
    sse_df['ask'] = sse_df['ask'].apply(lambda x: float(x)/1000 )

    sse_df['asize']  = pd.to_numeric(sse_df.iloc[:,40])
    sse_df['asize'] = sse_df['asize'].apply(lambda x: int(x)/1000 )

    sse_df['bid']    = pd.to_numeric(sse_df.iloc[:,37])
    sse_df['bid'] = sse_df['bid'].apply(lambda x: float(x)/1000 )

    sse_df['bsize']  = pd.to_numeric(sse_df.iloc[:,38])
    sse_df['bsize'] = sse_df['bsize'].apply(lambda x: int(x)/100 )

    sse_df = sse_df.drop(sse_df[sse_df.time == '0' ].index)
    print(sse_df)
    sse_df.iloc[:,7] = sse_df.iloc[:, 7].apply(lambda x:  '0'+x if len(x) < 2 else x)
    
    #sse_df['mon'] = sse_df['mon'].apply(lambda x:  '0'+x if len(x) < 2 else x)
    print(sse_df)
       
    sse_df['time'] = sse_df['time'].apply(lambda x: time_form(x))
    sse_df['nano'] = sse_df.iloc[:, 6] + '-' + sse_df.iloc[:, 7] + '-' + sse_df.iloc[:, 8] + ' ' + sse_df['time']
   # sse_df['nano'] = sse_df['nano'].apply( lambda x : time_to_micro(x) ) 
    
    #sse_df= sse_df.apply(lambda x:  x.split(' ')[0] if x.name in ['nano'] else x, axis = 1 )
    #sse_df['nano']= sse_df.apply(lambda x:  x['nano'].split(',')[0] +' ' +  x['nano'].split(',')[1][0:2]                            , axis = 1 )
    #sze_df = pd.read_csv(stock_path_sze, sep=',', dtype={'ticker':object}) if os.path.exists(stock_path_sze) and os.path.getsize(stock_path_sze) else None
    
   # sse_out = sse_df[['ticker', 'last_price', 'volume', 'bid', 'bsize', 'ask', 'asize', 'nano']]
    header = ['ticker', 'last_price', 'volume', 'bid', 'bsize', 'ask', 'asize', 'nano']
    sse_df.to_csv('20220712' +  '.csv', columns=header, index = False)       
    print(sse_df)


def form_sze(sze_file):
    sse_df = pd.read_csv(sze_file,header = None, sep=',', dtype=str) 
    sse_df['time'] = sse_df.iloc[:, 8]
    sse_df['nano'] = sse_df['time'].apply(lambda x: sze_time_to_micro(x))
    sse_df['ticker'] = sse_df.iloc[:,6]
    sse_df['ticker'] = sse_df['ticker'].apply(lambda x: 'sz.' + x)
    #sse_df['last_price'] = sse_df.iloc[:,17].apply()
    sse_df['last_price'] = pd.to_numeric(sse_df.iloc[:,17])
    sse_df['last_price'] = sse_df['last_price'].apply(lambda x: float(x)/10000 )

    sse_df['volume'] = pd.to_numeric(sse_df.iloc[:,14]) 
    sse_df['volume'] = sse_df['volume'].apply(lambda x: int(x)/100 )

    sse_df['ask']   = pd.to_numeric( sse_df.iloc[:,33])
    sse_df['ask'] = sse_df['ask'].apply(lambda x: float(x)/10000 )

    sse_df['asize'] = pd.to_numeric( sse_df.iloc[:,34])
    sse_df['asize'] = sse_df['asize'].apply(lambda x: int(x)/100 )

    sse_df['bid']   = pd.to_numeric( sse_df.iloc[:,31])
    sse_df['bid'] = sse_df['bid'].apply(lambda x: float(x)/10000 )

    sse_df['bsize'] = pd.to_numeric( sse_df.iloc[:,32])
    sse_df['bsize'] = sse_df['bsize'].apply(lambda x: int(x)/100 )

    print(sse_df)
   
    #sse_df= sse_df.apply(lambda x:  x.split(' ')[0] if x.name in ['nano'] else x, axis = 1 )
    #sse_df['nano']= sse_df.apply(lambda x:  x['nano'].split(',')[0] +' ' +  x['nano'].split(',')[1][0:2]                            , axis = 1 )
    
   

 
    headers = ['ticker', 'last_price', 'volume', 'bid', 'bsize', 'ask', 'asize', 'nano']
    sse_out.to_csv('20220712' +  '.csv', mode = 'a', columns = headers, header= False, index = False)       
    print(sse_df)



form_sse(stock_path_sse)
form_sze(stock_path_sze)
#form_sse(stock_path_sse)

## 处理上交股票
#if not sse_df is None:
#    sse_df = sse_df[['ticker', 'last_price', 'volume', 'bid', 'bsize', 'ask', 'asize', 'h_nano_s']]
#    sse_df.columns = ['ticker', 'last', 'vol', 'bid', 'bsize', 'ask', 'asize', 'date']
#    sse_df['date'] = pd.to_datetime(args.date + ' ' +sse_df['date'])
#    sse_stock_df = sse_df.loc[sse_df.ticker.str[3:].isin(tickers)]
#    print("SSE Stock ticker n: ", len(sse_stock_df.ticker.unique()))
#    setf_df = sse_df.loc[(sse_df.ticker.str[3:].isin(etf)) | (sse_df.ticker.str.startswith("sh.5"))]
#    print("SSE ETF ticker n: ", len(setf_df.ticker.unique()))
#    sse_bond_df = sse_df.loc[sse_df.ticker.str.startswith("sh.110") | sse_df.ticker.str.startswith("sh.113")]
#    print("SSE Bond ticker n: ", len(sse_bond_df.ticker.unique()))
#else:
#    sse_stock_df, setf_df, sse_bond_df = None, None, None
#
## 处理深交股票
#if not sze_df is None:
#    sze_df = sze_df[['ticker', 'last_price', 'volume', 'bid', 'bsize', 'ask', 'asize', 'h_nano_s']]
#    sze_df.columns = ['ticker', 'last', 'vol', 'bid', 'bsize', 'ask', 'asize', 'date']
#    sze_df['date'] = pd.to_datetime(args.date + ' ' +sze_df['date'])
#    sze_stock_df = sze_df.loc[sze_df.ticker.str[3:].isin(tickers)]      # 筛选出沪深300和中证500的股票，以及其他要求的ticker
#    print("SZE Stock ticker n: ", len(sze_stock_df.ticker.unique()))    
#    zetf_df = sze_df.loc[(sze_df.ticker.str[3:].isin(etf)) | (sze_df.ticker.str.startswith("sz.15"))]   # 筛选出ETF股票
#    print("SZE ETF ticker n: ", len(zetf_df.ticker.unique()))
#    sze_bond_df = sze_df.loc[sze_df.ticker.str.startswith("sz.123") | sze_df.ticker.str.startswith("sz.127") | sze_df.ticker.str.startswith("sz.128")]  # 筛选出可转债股票
#    print("SZE Bond ticker n: ", len(sze_bond_df.ticker.unique()))
#else:
#    sze_stock_df, zetf_df, sze_bond_df = None, None, None
#
## stock, etf, bond交易量及成交额汇总
#stock_df = pd.concat([sse_stock_df, sze_stock_df])
#stock_sum = summary(stock_df)
#stock_sum.to_csv("stock.csv", index=False)
#etf_df = pd.concat([setf_df, zetf_df])
#etf_sum = summary(etf_df)
#etf_sum.to_csv("etf.csv", index=False)
#bond_df = pd.concat([sse_bond_df, sze_bond_df])
#bond_sum = summary(bond_df)
#bond_sum.to_csv('bond.csv', index=False)
#
#print("stock etf  bond\n")
#print(time.strftime('%H:%M:%S',time.localtime(time.time())))
#
#
#
## 合并
#dlist, tlist, mlist = [], [], []
#tbl = list(zip([fut_df, cffex_df, sseop_df, szeop_df, sse_stock_df, setf_df, sse_bond_df, sze_stock_df, zetf_df, sze_bond_df], ['ICFH', 'IO', 'SSE Option', 'SZE Option', 'SSE Stock', 'SSE ETF', 'SSE Bond', 'SZE Stock', 'SZE ETF', 'SZE Bond']))
#for x in tbl:
#    if not x[0] is None:
#        dlist.append(x[0])
#        tlist.append(x[1])
#    else:
#        mlist.append(x[1])
#
#
#df = pd.concat(dlist)
#df.to_csv(date+'.csv', index=False)
#
#
#print("all concat\n")
#print(time.strftime('%H:%M:%S',time.localtime(time.time())))
#
#
#
#
#iline = date + '    generated types:    ' + ' | '.join(tlist) + '\n'
#with open(exec_path + "info.txt", "a") as f:
#    f.writelines(iline)
#iline = date + '    missing types:    ' + ' | '.join(mlist) + '\n'
#with open(exec_path + "missing.txt", "a") as f:
#    f.writelines(iline)
#
#
#print("concat done. written to "+date+'.csv')
